package com.nimitz.modules.approval.controller;


import com.nimitz.common.core.web.controller.BaseController;
import com.nimitz.common.core.web.domain.AjaxResult;
import com.nimitz.modules.approval.domain.ApprovalInstance;
import com.nimitz.modules.approval.service.IApprovalInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ApprovalInstanceController
 * @Author FuCun
 * @Date 2021-08-17-14:45
 * @PackageName com.nimitz.modules.approval.controller
 * @Description 审批实例
 * @Version 1.0.0
 */
@RestController
@Api(tags = "审批实例信息")
@RequestMapping("/approval-instance")
public class ApprovalInstanceController extends BaseController {
    @Autowired
    private IApprovalInstanceService approvalInstanceService;

    /**
     * 查询所有审批实例信息（含搜索）
     *
     * @param approvalInstance 审批实例信息
     * @return 审批实例信息集合
     */
    @GetMapping("/instanceList")
    @ApiOperation(value = "查询所有审批实例信息（含搜索）")
    public AjaxResult list(ApprovalInstance approvalInstance){
        List<ApprovalInstance> approvalInstances = approvalInstanceService.selectApprovalInstanceList(approvalInstance);
        System.out.println(approvalInstances);
        return AjaxResult.success(approvalInstances);
    }

    /**
     * 新增审批实例
     *
     * @param approvalInstance 审批实例
     * @return 结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增审批实例")
    public AjaxResult add(@RequestBody ApprovalInstance approvalInstance){
        ApprovalInstance approvalUserCommon = approvalInstanceService.checkApprovalInstanceIdUnique(approvalInstance.getInstanceNo());
        if(approvalUserCommon!=null){
            if (approvalUserCommon.getApprovalNo().equals(approvalInstance.getApprovalNo())){
                return AjaxResult.error("新增"+approvalInstance.getApprovalNo()+"失败");
            }
        }
        return toAjax(approvalInstanceService.insertApprovalInstance(approvalInstance));
    }

    /**
     * 修改审批实例状态
     *
     * @param approvalInstance 审批实例
     * @return 结果
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改审批实例")
    public AjaxResult edit(@RequestBody ApprovalInstance approvalInstance){
        return toAjax(approvalInstanceService.updateApprovalInstance(approvalInstance));
    }

    /**
     * 删除审批实例
     *
     * @param approvalInstanceId 审批实例id
     * @return 结果
     */

    @DeleteMapping("/{approvalInstanceId}")
    @ApiOperation(value = "删除流程节点审核批示")
    public AjaxResult remove(@PathVariable Long approvalInstanceId) {
        return toAjax(approvalInstanceService.deleteApprovalInstanceByInstanceId(approvalInstanceId));
    }
}
